Thread: Segmentation Fault argc and argv[] become zero after function call.

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    33

    Arrow Segmentation Fault argc and argv[] become zero after function call.

    I'm using gdb in an attempt to debug my program. gdb has made me aware that line 17 is where the fault occurs. For some reason my input parameters for main become zero.

    Breakpoint 2, main (argc=0, argv=0x401c0000) at polygon.c:17
    17 fclose(fp);



    Consequently Line 18 can't access them. I tried to print the variables, and received the warning "Cannot access memory at address 0x401c0008C" I'm stumped. I have no idea why it's not working. Please help
    Code:
      
        # include<stdio.h># define MAXIMUM_SIZE 50
    
    
    int get_corners(FILE *fp,
         double matrix_x[MAXIMUM_SIZE], double
         matrix_y[MAXIMUM_SIZE]);
    void output_corners(FILE *fp, double array1[], double array2[], int actual_size);
    
    
    int main( int argc, char *argv[])
    
    
    {
      FILE* fp;
      int actual_size;
      double matrix_x[MAXIMUM_SIZE], matrix_y[MAXIMUM_SIZE];
      fp = fopen(argv[1], "r");
      actual_size = get_corners( fp, &matrix_x[MAXIMUM_SIZE], &matrix_y[MAXIMUM_SIZE]);
      fclose(fp);
      fp = fopen(argv[2], "w");
      output_corners( fp, &matrix_x[MAXIMUM_SIZE], &matrix_y[MAXIMUM_SIZE], actual_size);
      return(0);
    }
    
    
    int get_corners(FILE *fp,
          double matrix_x[MAXIMUM_SIZE], double matrix_y[MAXIMUM_SIZE])
    
    
    {
      double x_cord, y_cord;
      int i = 0, input_status, matrix_size;
      fscanf(fp, "%lf", &x_cord);
      input_status = fscanf(fp, "%lf", &y_cord);
      matrix_size = 0;
      while (input_status != EOF ) {
           printf("%lf\n", x_cord);
        matrix_x[i] = x_cord;
        matrix_y[i] = y_cord;
        i = i + 1;
        input_status = fscanf(fp, "%lf", &x_cord);
        fscanf(fp, "%lf", &y_cord);
        matrix_size += 1;
        };
    return (matrix_size);
    }
    
    
    void output_corners(FILE *fp, double matrix_x[], double matrix_y[], int actual_size)
    
    
    {
    
    
      int i;
    
    
      for (i = 0 ; i < ( actual_size - 1 ) ;
        ++i){
        fprintf(fp, "%.1lf %.1lf\n", matrix_x[i], matrix_y[i]);
        }
      printf("%lf", matrix_x[1]);
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Did you check argc? Is it greater than one?
    Did you check fp?Is it different than NULL?

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    357
    Code:
    int main( int argc, char *argv[])
     
     
    {
      FILE* fp;
      int actual_size;
      double matrix_x[MAXIMUM_SIZE], matrix_y[MAXIMUM_SIZE];
      fp = fopen(argv[1], "r");
    
      // WRONG!
      actual_size = get_corners( fp, &matrix_x[MAXIMUM_SIZE], &matrix_y[MAXIMUM_SIZE]);
      fclose(fp);
      fp = fopen(argv[2], "w");
    
      // WRONG!!
      output_corners( fp, &matrix_x[MAXIMUM_SIZE], &matrix_y[MAXIMUM_SIZE], actual_size);
      return(0);
    }
    Try this
    Code:
    int main( int argc, char *argv[])
     
     
    {
      FILE* fp;
      int actual_size;
      double matrix_x[MAXIMUM_SIZE], matrix_y[MAXIMUM_SIZE];
      fp = fopen(argv[1], "r");
    
      // no &, no indexing
      actual_size = get_corners( fp, matrix_x, matrix_y);
      fclose(fp);
      fp = fopen(argv[2], "w");
    
      // no &, no indexing
      output_corners( fp, matrix_x, matrix_y, actual_size);
      return(0);
    }

  4. #4
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    As an explanation to qny's suggestion, this line of code
    Quote Originally Posted by Kurtanius21 View Post
    Code:
      actual_size = get_corners( fp, &matrix_x[MAXIMUM_SIZE], &matrix_y[MAXIMUM_SIZE]);
    means
    Code:
      actual_size = get_corners(fp, after the final element of matrix_x array, after the final element of matrix_y array);
    In other words, you're not supplying the arrays as parameters, but pointers to the data that follows the arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to use argc and argv
    By Salahuddin in forum C Programming
    Replies: 19
    Last Post: 09-09-2011, 03:53 AM
  2. argc, **argv in C++.
    By apacz in forum C++ Programming
    Replies: 8
    Last Post: 01-05-2007, 12:09 AM
  3. argc & argv
    By miryellis in forum C Programming
    Replies: 11
    Last Post: 09-19-2004, 11:59 PM
  4. argc--; argv++;
    By C of Green in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2003, 06:16 PM
  5. argc,argv
    By thanatos in forum C Programming
    Replies: 5
    Last Post: 05-31-2002, 01:26 AM